const state = {
  options: [{ route: '/home', name: 'MainPage' }],
  activeIndex: '/home'
}
const mutations = {
  SET_OPTIONS: (state, data) => {
    state.options = data
  },
  // 添加 tabs
  ADD_TABS: (state, data) => {
    //state.options.findIndex(m => m.name === data.name) < 0 && state.options.push(data)
    state.options.push(data);
    localStorage.setItem("menuOptions", JSON.stringify(state.options))
  },
  // 删除 tabs
  DELETE_TABS: (state, route) => {
    let index = 0;
    for (let option of state.options) {
      if (option.route === route) {
        break;
      }
      index++;
    }
    state.options.splice(index, 1);
    localStorage.setItem("menuOptions", JSON.stringify(state.options))
  },
  // 设置当前激活的 tab,route
  SET_ACTIVEI_NDEX: (state, index) => {
    state.activeIndex = index;
    localStorage.setItem("menuIndex", state.activeIndex)
  },
}
const actions = {
  addTabs({ commit }, info) {
    commit('ADD_TABS', info)
  },
  deleteTabs({ commit }, info) {
    commit('DELETE_TABS', info)
  },
  setActiveIndex({ commit }, info) {
    commit('SET_ACTIVEI_NDEX', info)
  },
}


export default {
  namespaced: true,
  state,
  mutations,
  actions
}
